package com.ldapauth.abstracts;

import cn.hutool.core.collection.CollectionUtil;

import java.util.List;

/**
 * 抽象类 定义公告方法
 */
public abstract class CommonTablesAbstracts {

    /**
     * 构建查询列
     * @param selectColumnNames
     * @return
     */
    public String buildSelectCoulunm(List<String> selectColumnNames){
        String  selectColumns = "*";
        if (CollectionUtil.isNotEmpty(selectColumnNames)) {
            int i = 0;
            for(String name : selectColumnNames) {
                if ("*".equals(name)) {
                    selectColumns = "*";
                    break;
                } else {
                    if (i==0) {
                        selectColumns = name;
                    } else {
                        selectColumns += "," + name;
                    }
                }
                i++;
            }
        } else {
            selectColumns = "*";
        }
        return selectColumns;
    }

    /**
     * 构建更新列
     * @param selectColumnNames
     * @return
     */
    public String buildSetColumns(List<String> selectColumnNames) {
        String updateColumns = null;
        int i = 0;
        for (String name : selectColumnNames) {

            if (i == 0) {
                updateColumns = name+"=?";
            } else {
                updateColumns += ","+name+"=?";
            }
            i++;
        }
        return updateColumns;
    }



    /**
     * 构建插入列
     * @param insertColumnNames
     * @param type 0=构建插入列，1=构建占位符
     * @return
     */
    public String buildInsertSetColumns(List<String> insertColumnNames,int type) {
        String columns = null;
        int i = 0;
        for (String name : insertColumnNames) {
            if (type == 0) {
                if (i == 0) {
                    columns = name;
                } else {
                    columns += "," + name;
                }
            } else {
                if (i == 0) {
                    columns = "?";
                } else {
                    columns += ",?";
                }
            }
            i++;
        }
        return columns;
    }

}
